<div class="tutorial-bundle" data-metadata-json='{"groupedProjectAssets": [{"name": "Cartoon moles", "assets": ["no-moles.png", "mole-left.png", "mole-centre.png", "mole-right.png"]}, {"name": "Single mole", "assets": ["one-hole.png", "one-mole.png"]}], "programKind": "per-method", "difficulty": "medium"}' data-tip-sha1="c3f4d36f796d481d2db3149340a5aab69197f97d"><div class="front-matter" data-complete-code-text='import pytch
import random
import math


class Stage(pytch.Stage):
    Backdrops = ["cartoon-field.png"]


class Mole(pytch.Sprite):
    Costumes = ["no-moles.png", "mole-left.png", "mole-centre.png", "mole-right.png"]

    @pytch.when_green_flag_clicked
    def run(self):
        self.go_to_xy(0, -100)

        while True:
            costume_index = random.randint(1, 3)
            self.switch_costume(costume_index)
            above_ground_time = random.uniform(0.5, 1.0)
            pytch.wait_seconds(above_ground_time)
            self.switch_costume("no-moles.png")
            under_ground_time = random.uniform(0.5, 1.0)
            pytch.wait_seconds(under_ground_time)

    @pytch.when_green_flag_clicked
    def init_score(self):
        self.score = 0
        pytch.show_variable(self, "score")

    @pytch.when_key_pressed("j")
    def hit_left(self):
        if self.costume_number == 1:
            self.score += 1
            self.switch_costume("no-moles.png")
        else:
            self.score = 0

    @pytch.when_key_pressed("k")
    def hit_centre(self):
        if self.costume_number == 2:
            self.score += 1
            self.switch_costume("no-moles.png")
        else:
            self.score = 0

    @pytch.when_key_pressed("l")
    def hit_right(self):
        if self.costume_number == 3:
            self.score += 1
            self.switch_costume("no-moles.png")
        else:
            self.score = 0
' data-initial-code-text='import pytch
import random
import math


class Stage(pytch.Stage):
    Backdrops = ["solid-white.png"]
'><h1>Script-by-script splat the moles</h1><p>In this tutorial, you’ll write a game in Python where the player has
to splat moles to score points.  But if they miss, they lose all their
points!</p><p>(Don't worry, no real moles will be harmed in making this game.)</p><p><img alt="screenshot" src="screenshot-w360.png#img-center"/></p></div><div class="chapter-content"><h2>Give the stage a backdrop</h2><p>When you create a project, the stage has a plain backdrop.  This game
needs a better backdrop.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add the “cartoon field” backdrop to the stage.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-medialib-appearances-entry", "actor": {"kind": "stage"}, "displayIdentifier": "cartoon-field.png", "nItems": 1}' data-slug="add-field-backdrop"></div>
</div></div><p>We don’t need the plain backdrop any more.</p><div class="learner-task"><div class="learner-task-intro">
<p>Delete the “solid white” backdrop from the stage.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "delete-appearance", "actor": {"kind": "stage"}, "appearanceFilename": "solid-white.png"}' data-slug="remove-default-backdrop"></div>
</div></div><p>It’s a good idea to test your project as you go along.</p><div class="learner-task"><div class="learner-task-intro">
<p>Run your project using the green button above the stage.  Check that
the backdrop is the “cartoon field” one.</p>
</div></div></div><div class="chapter-content"><h2>Add a sprite for the mole</h2><p>There are a few different ways the moles could work.  For this
tutorial, you’ll have one sprite with four different costumes
depending on whether the mole is completely underground, or poking up
from one of the three holes.  By making the sprite wear different
costumes, you can make it look like the mole is choosing different
holes to pop out of.</p><p>First we need a Sprite for the mole and its holes.</p><div class="learner-task"><div class="learner-task-intro">
<p>Create a new sprite, and call it “Mole”.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-sprite", "name": "Mole"}' data-slug="create-Mole-sprite"></div>
</div></div><p>The Mole sprite needs its four costumes.  These are in Pytch’s media
library.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add the “Cartoon moles” image bundle as costumes of your new Mole
sprite.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-medialib-appearances-entry", "actor": {"kind": "sprite", "name": "Mole"}, "displayIdentifier": "Cartoon moles", "nItems": 4}' data-slug="add-Mole-costumes"></div>
</div></div><p>Remeber to test your game as you go along!</p><div class="learner-task"><div class="learner-task-intro">
<p>Run your project using the green button above the stage.  Check that
a row of empty mole-holes appears in the field.</p>
</div></div><h3>Put the mole-holes in a better position</h3><p>This looks alright, but the holes are a bit high.  To move them lower,
you need to write some code that runs as soon as the green flag is
clicked.  In Scratch, you might do something like</p><pre><code class="language-scratch">when green flag clicked:
  go to x: (0) y: (-100)
</code></pre><p>where the <code>x: 0</code> means the sprite should be centred left-to-right, and
the <code>y: -100</code> means sprite should be a bit lower than the centre
top-to-bottom.</p><p>In Pytch it’s very similar.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a “when green flag clicked” script to your Mole sprite.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "run"}, "event": {"kind": "green-flag"}, "codeText": ""}' data-slug="add-Mole-green-flag-script"></div>
</div></div><p>Now you can write the code to move the mole-holes.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add code to this script to move the Mole to <code>(0, -100)</code>.</p>
</div><div class="learner-task-help">
<p>You can use the help bar at the left to learn what the Pytch
equivalent of Scratch's</p>
<pre><code class="language-scratch">go to x: () y: ()
</code></pre>
<p>block is, and how to say what <em>x</em> and <em>y</em> values you want.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "run"}, "event": {"kind": "green-flag"}, "oldCodeText": "", "newCodeText": "self.go_to_xy(0, -100)"}' data-slug="init-Mole-position"></div>
</div></div><h3>Test it!</h3><div class="learner-task"><div class="learner-task-intro">
<p>Run your project.  Check the row of mole-holes is in a good position.</p>
</div></div></div><div class="chapter-content"><h2>Pop out of random holes</h2><p>Now you need to make the mole appear out of one of the holes.  If you
look in the “Costumes” tab, you’ll see which costumes have the mole
popping out of a hole.</p><p>Pytch stores a sprite’s costumes in a <em>list</em>, and describes the
position of an entry of a list using numbers <em>starting from zero</em>.  A
number used to point at an entry of a list is often called an <em>index</em>.
So you need to choose a “costume index” randomly between <code>1</code> and <code>3</code>
(inclusive).</p><p>One good use of <em>variables</em> is to give things names, so that your
program is easier to understand when a human is reading it.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add code which chooses a random whole number between <code>1</code> and <code>3</code>
(inclusive), and stores it in a variable <code>costume_index</code>.</p>
</div><div class="learner-task-help">
<p>In Scratch you might use</p>
<pre><code class="language-scratch">(pick random (1) to (3))
</code></pre>
<p>to get a random number from <code>1</code> to <code>3</code>.  You can use the help to find
out how to do this in Python.</p>
</div><div class="learner-task-help">
<p>To create a variable in Python, you use <code>=</code>.  For example, here’s how
to make the variable <code>health</code> have the value <code>150</code>:</p>
<pre><code class="language-python">health = 150
</code></pre>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "run"}, "event": {"kind": "green-flag"}, "oldCodeText": "self.go_to_xy(0, -100)", "newCodeText": "self.go_to_xy(0, -100)\n\ncostume_index = random.randint(1, 3)"}' data-slug="choose-random-hole-costume"></div>
</div></div><p>Now your program has chosen a costume, you can switch to it.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a line of code which makes the Mole switch to the costume you have
chosen.</p>
</div><div class="learner-task-help">
<p>You have just made the variable <code>costume_index</code> hold the number of the
costume the Mole should switch to.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "run"}, "event": {"kind": "green-flag"}, "oldCodeText": "self.go_to_xy(0, -100)\n\ncostume_index = random.randint(1, 3)", "newCodeText": "self.go_to_xy(0, -100)\n\ncostume_index = random.randint(1, 3)\nself.switch_costume(costume_index)"}' data-slug="switch-to-chosen-costume"></div>
</div></div><h3>Test it!</h3><div class="learner-task"><div class="learner-task-intro">
<p>Run your program a few times.  Check the mole pops out of random
holes.</p>
</div></div></div><div class="chapter-content"><h2>Keep popping out of holes</h2><p>The mole should <em>repeatedly</em> pop out of a random hole, not just once.
In scratch you might put the blocks</p><pre><code class="language-scratch">set [costume_index v] to (pick random (1) to (3))
switch costume to (costume_index)
</code></pre><p>“inside a forever”, like this:</p><pre><code class="language-scratch">forever
  set [costume_index v] to (pick random (1) to (3))
  switch costume to (costume_index)
</code></pre><p>In Python, you can use <code>while True:</code> to do the same job.  Putting code
“inside” means <em>indenting</em> it four spaces.</p><div class="learner-task"><div class="learner-task-intro">
<p>Move the two lines of code which choose and switch to a random costume
inside a <code>while True:</code> loop.</p>
</div><div class="learner-task-help">
<p>There is an example of what this looks like in the help.  You will
need to add a line, and <em>re-indent</em> two existing lines.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "run"}, "event": {"kind": "green-flag"}, "oldCodeText": "self.go_to_xy(0, -100)\n\ncostume_index = random.randint(1, 3)\nself.switch_costume(costume_index)", "newCodeText": "self.go_to_xy(0, -100)\n\nwhile True:\n    costume_index = random.randint(1, 3)\n    self.switch_costume(costume_index)"}' data-slug="iterate-switching-costume"></div>
</div></div><h3>Test it!</h3><div class="learner-task"><div class="learner-task-intro">
<p>Run your program.  What happens?</p>
</div></div><p>It <em>sort of</em> works.  The problem is that it’s switching costume so
quickly, the player would have no chance.</p><h3>Make the mole wait when it’s popped up</h3><p>The mole should wait a random amount of time after switching to the
chosen costume.  The code will be quite similar to the code for
choosing and switching to a random costume.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add code, just below where you switch costume, which chooses a random
number between <code>0.5</code> and <code>1.0</code> (inclusive), and stores it in a
variable <code>above_ground_time</code>.</p>
<p>Instead of <code>random.randint()</code>, you can use <code>random.uniform()</code> to ask
Python to randomly choose a number with a fractional part.</p>
</div><div class="learner-task-help">
<p>To choose a random number between <code>0.5</code> and <code>1.0</code>, you can use the
Python expression</p>
<pre><code class="language-python-expression">random.uniform(0.5, 1.0)
</code></pre>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "run"}, "event": {"kind": "green-flag"}, "oldCodeText": "self.go_to_xy(0, -100)\n\nwhile True:\n    costume_index = random.randint(1, 3)\n    self.switch_costume(costume_index)", "newCodeText": "self.go_to_xy(0, -100)\n\nwhile True:\n    costume_index = random.randint(1, 3)\n    self.switch_costume(costume_index)\n    above_ground_time = random.uniform(0.5, 1.0)"}' data-slug="choose-wait-time"></div>
</div></div><p>Now you can make the program wait before going round the loop and
choosing another costume.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add another line which makes the mole wait for the number of seconds
in the <code>above_ground_time</code> variable.</p>
</div><div class="learner-task-help">
<p>You can use the help to find what to say in Python to match Scratch’s</p>
<pre><code class="language-scratch">wait () seconds
</code></pre>
<p>block.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "run"}, "event": {"kind": "green-flag"}, "oldCodeText": "self.go_to_xy(0, -100)\n\nwhile True:\n    costume_index = random.randint(1, 3)\n    self.switch_costume(costume_index)\n    above_ground_time = random.uniform(0.5, 1.0)", "newCodeText": "self.go_to_xy(0, -100)\n\nwhile True:\n    costume_index = random.randint(1, 3)\n    self.switch_costume(costume_index)\n    above_ground_time = random.uniform(0.5, 1.0)\n    pytch.wait_seconds(above_ground_time)"}' data-slug="wait-for-chosen-time"></div>
</div></div><h3>Test it!</h3><div class="learner-task"><div class="learner-task-intro">
<p>Run your program.  You should see the mole randomly switching between
holes, waiting a short time above ground each time.</p>
</div></div></div><div class="chapter-content"><h2>Hide underground before popping back up</h2><p>The game is better if the mole hides underground for a bit in between
popping up from random holes.  You can do this by making it switch to
the <code>"no-moles.png"</code> costume and then waiting for a short random
amount of time.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add three lines of code which:</p>
<ul>
<li>Switch to the <code>"no-moles.png"</code> costume.</li>
<li>Choose a random number from <code>0.5</code> to <code>1.0</code> and store it in a
  variable <code>under_ground_time</code>.</li>
<li>Wait for the amount of time in the <code>under_ground_time</code> variable.</li>
</ul>
</div><div class="learner-task-help">
<p>The code will be similar to the lines we just wrote which switch to a
costume, choose a random over-ground time, and wait for that time.</p>
</div><div class="learner-task-help">
<p>There are two ways to switch to the no-moles costume.  You can either
give the <em>name</em> of the costume (<code>"no-moles.png"</code>) or its <em>index</em>
(<code>0</code>).  Which do you prefer?</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "run"}, "event": {"kind": "green-flag"}, "oldCodeText": "self.go_to_xy(0, -100)\n\nwhile True:\n    costume_index = random.randint(1, 3)\n    self.switch_costume(costume_index)\n    above_ground_time = random.uniform(0.5, 1.0)\n    pytch.wait_seconds(above_ground_time)", "newCodeText": "self.go_to_xy(0, -100)\n\nwhile True:\n    costume_index = random.randint(1, 3)\n    self.switch_costume(costume_index)\n    above_ground_time = random.uniform(0.5, 1.0)\n    pytch.wait_seconds(above_ground_time)\n    self.switch_costume(\"no-moles.png\")\n    under_ground_time = random.uniform(0.5, 1.0)\n    pytch.wait_seconds(under_ground_time)"}' data-slug="hide-underground-for-random-time"></div>
</div></div><h3>Test it!</h3><div class="learner-task"><div class="learner-task-intro">
<p>Run your program.  You should see the mole popping out of a random
hole for a short while, then hiding underground for a short while,
then doing this again and again.</p>
</div></div></div><div class="chapter-content"><h2>Scoring</h2><p>Now you’ll start making the code which lets the player try to splat the
mole.  The <code>Mole</code> sprite will keep score — how many times the player
has managed to splat it.</p><p>To keep track of something, your program will use a <em>variable</em>.  It
will be like a “For this sprite only” variable in Scratch.  In Python,
to create a variable, you just set it to a value.  There’s no separate
step to create a variable.</p><p>At the very start of the game, the score should be set to zero,
because the player hasn’t splatted any moles yet.</p><p>You could put this code in the same green-flag script which already
exists, but it’s a good idea to keep the different parts of your
program separate, so it’s easier to understand.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a new green-flag script to your Mole.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "init_score"}, "event": {"kind": "green-flag"}, "codeText": ""}' data-slug="add-init-score-script"></div>
</div></div><p>Now you can write the code to <em>initialise</em> the score.</p><div class="learner-task"><div class="learner-task-intro">
<p>Assign the value <code>0</code> to a variable <code>self.score</code>.  The <code>self.</code> at the
start of <code>self.score</code> is what makes it a “For this sprite only”
variable.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "init_score"}, "event": {"kind": "green-flag"}, "oldCodeText": "", "newCodeText": "self.score = 0"}' data-slug="init-score-to-zero"></div>
</div></div><p>If you run the game, you won’t see anything different happen, because
the program doesn’t do anything with the <code>Mole</code> sprite’s <code>score</code>
variable.  In Scratch, you could display the score by checking the
check-box next to the variable, or by using this block:</p><pre><code class="language-scratch">show variable [score v]
</code></pre><p>Pytch does have a command like that block, but it needs a bit more
information: You have to say who owns the variable.  The <code>score</code>
variable belongs to the <code>Mole</code> sprite, which as you’ve seen, is called
<code>self</code> inside its scripts.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a line of code which uses Pytch’s <code>pytch.show_variable()</code> to
display the score.</p>
</div><div class="learner-task-help">
<p>The help gives an example.  Try to adapt it to what you’re trying to
do here.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "init_score"}, "event": {"kind": "green-flag"}, "oldCodeText": "self.score = 0", "newCodeText": "self.score = 0\npytch.show_variable(self, \"score\")"}' data-slug="show-score"></div>
</div></div><h3>Test it!</h3><div class="learner-task"><div class="learner-task-intro">
<p>Run your program.  You should see the score displayed on the stage.</p>
</div></div></div><div class="chapter-content"><h2>Let the player splat the moles</h2><p>The game will be controlled by the keyboard.  The player will:</p><ul>
<li>press <code>j</code> to hit the left hole;</li>
<li>press <code>k</code> to hit the centre hole;</li>
<li>press <code>l</code> to hit the right hole.</li>
</ul><p>You’ll write code for <code>j</code> (the left hole) first, and then adapt it for
the other two keys.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a script to the Mole which runs when the player presses the <code>j</code>
key.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "hit_left"}, "event": {"kind": "key-pressed", "keyName": "j"}, "codeText": ""}' data-slug="add-left-mole-click-script"></div>
</div></div><p>When the player presses <code>j</code>, if the mole is popped out of the left
hole, the score should go up by <code>1</code>.  Remember that “popped out of the
left hole” is the same as “wearing costume number 1”.</p><p>In Scratch, you might write code like this:</p><pre><code class="language-scratch">if &lt;(costume [number v]) = (1)&gt; then
  change [score v] by (1)
</code></pre><div class="learner-task"><div class="learner-task-intro">
<p>Write Python code in your new script which does the same job.  Use the
help to look up the Python version of the two Scratch blocks above.</p>
</div><div class="learner-task-help">
<p>In Python, you need <em>two</em> equals signs to test whether two things are
the same.  So to ask whether the Mole’s costume-number is <code>1</code>, you can
use the Python expression</p>
<pre><code class="language-python-expression">self.costume_number == 1
</code></pre>
</div><div class="learner-task-help">
<p>To add one to a variable, you can use Python’s <code>+=</code> operator.  For
example, to add one to a sprite’s <code>health</code> variable, you could do</p>
<pre><code class="language-python">self.health += 1
</code></pre>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "hit_left"}, "event": {"kind": "key-pressed", "keyName": "j"}, "oldCodeText": "", "newCodeText": "if self.costume_number == 1:\n    self.score += 1"}' data-slug="point-for-left-hit"></div>
</div></div><h3>Test it!</h3><div class="learner-task"><div class="learner-task-intro">
<p>Run your program.  You should see the score go up if you press <code>j</code>
while the mole is popped out of the left hole.  But if the mole is
<em>not</em> popped out of the left-hand hole, nothing should happen when you
press <code>j</code>.</p>
</div></div><h3>What if the player misses?</h3><p>But this is now too easy.  The player can just keep pressing <code>j</code> and
whenever the mole pops out of the left hole, the player scores a
point.  There needs to be a way to discourage the player from doing
this.</p><p>Let’s be harsh — if the player misses, they lose all their points.
This should happen if the player presses <code>j</code> but the <code>Mole</code> sprite is
<em>not</em> wearing costume number <code>1</code>.  You need something like Scratch’s</p><pre><code class="language-scratch">if &lt;&gt; then
else
end
</code></pre><p>block.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add an <em>else</em> clause to your <code>if</code> statement which sets the score to
zero if the Mole is not popped out of the left hole when the player
presses <code>j</code>.</p>
</div><div class="learner-task-help">
<p>You can find an example in the help.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "hit_left"}, "event": {"kind": "key-pressed", "keyName": "j"}, "oldCodeText": "if self.costume_number == 1:\n    self.score += 1", "newCodeText": "if self.costume_number == 1:\n    self.score += 1\nelse:\n    self.score = 0"}' data-slug="lose-points-left-miss"></div>
</div></div><p>You might think this is a bit <em>too</em> harsh!  Have a look at the
<em>Challenges</em> at the end of this tutorial for some other ideas.</p><h3>Make a splatted mole go back underground</h3><p>There’s still a small way the player can unfairly get more points.  If
they keep hitting <code>j</code> while the mole is popped up out of the left
hole, they can rack up points.</p><p>Let’s make the mole go back underground when the player splats it.  We
can do this just by making it switch back to the <code>"no-moles.png"</code>
costume.</p><div class="learner-task"><div class="learner-task-intro">
<p>Add a line of code (after the line which gives the player a point)
which switches the Mole’s costume to the <code>"no-moles.png"</code> one.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "edit-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "hit_left"}, "event": {"kind": "key-pressed", "keyName": "j"}, "oldCodeText": "if self.costume_number == 1:\n    self.score += 1\nelse:\n    self.score = 0", "newCodeText": "if self.costume_number == 1:\n    self.score += 1\n    self.switch_costume(\"no-moles.png\")\nelse:\n    self.score = 0"}' data-slug="hide-after-left-hit"></div>
</div></div></div><div class="chapter-content"><h2>Let the player splat the centre and right holes</h2><p>To finish the game, you just need to make two more scripts, very
similar to the one you’ve just made.</p><h3>Splat the centre hole</h3><p>First handle the centre hole.</p><div class="learner-task"><div class="learner-task-intro">
<p>Make a script which:</p>
<ul>
<li>runs when the player presses the <code>k</code> key;</li>
<li>has code which is almost the same as the <code>j</code> key script, but checks
  that the mole is popped up in the centre hole.</li>
</ul>
</div><div class="learner-task-help">
<p>You can use the “Duplicate script” entry in the dropdown menu of your
<code>j</code> script if you think that will be quicker than copying and pasting.</p>
</div><div class="learner-task-help">
<p>To check whether the mole is popped up in the centre hole, check
whether its costume number is <code>2</code>.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "hit_centre"}, "event": {"kind": "key-pressed", "keyName": "k"}, "codeText": "if self.costume_number == 2:\n    self.score += 1\n    self.switch_costume(\"no-moles.png\")\nelse:\n    self.score = 0"}' data-slug="copy-paste-for-centre"></div>
</div></div><h3>Splat the right hole</h3><p>And finally handle the right-hand hole.</p><div class="learner-task"><div class="learner-task-intro">
<p>Make a script which:</p>
<ul>
<li>runs when the player presses the <code>l</code> key;</li>
<li>has code which is almost the same as the <code>j</code> key script, but checks
  that the mole is popped up in the right-hand hole.</li>
</ul>
</div><div class="learner-task-help">
<p>To check whether the mole is popped up in the centre hole, check
whether its costume number is <code>3</code>.</p>
</div><div class="learner-task-help">
<div class="jr-commit" data-jr-commit='{"kind": "add-script", "path": {"actor": {"kind": "sprite", "name": "Mole"}, "methodName": "hit_right"}, "event": {"kind": "key-pressed", "keyName": "l"}, "codeText": "if self.costume_number == 3:\n    self.score += 1\n    self.switch_costume(\"no-moles.png\")\nelse:\n    self.score = 0"}' data-slug="copy-paste-for-right"></div>
</div></div><h3>Try the finished game!</h3><p>How many points can you get?</p></div><div class="chapter-content"><h2>Challenges and questions</h2><p>Maybe you can think of ways to make this game better.  Here are some
ideas:</p><ul>
<li>Find and use a different backdrop.  Add more than one backdrop, and
  change between them randomly, or as the player gets more points.</li>
<li>Perhaps it’s a bit harsh for the player to lose all their points
  when they miss.  Maybe they should lose some fixed number of points
  instead.  Change the code (in three places!) so that the player
  loses five points if they miss.  You can use the Python operator
  <code>-=</code> to subtract points.  What happens if they only have three
  points when they miss?</li>
<li>Instead of losing points, the player could start with three lives,
  and they lose a life every time they miss.  Hint: You can show a
  variable at the <em>right</em> of the screen with the code
  <code>pytch.show_variable(self, "lives", right=236)</code>.</li>
<li>Adjust the difficulty of the game by making the mole stay out of its
  hole for a longer or shorter time.  You could even make the game get
  more difficult as the player gets more points.</li>
</ul><h3>Other ways of doing things</h3><p>There is often more than one way to write a program.  Here are some
ways you could investigate writing the program differently.</p><ul>
<li>The <code>Mole</code> sprite has two green-flag scripts.  Can you combine them
  into one script?  Do you think the program is easier to understand
  with two green-flag scripts or one?</li>
<li><strong>Advanced:</strong> Instead of one <code>Mole</code> sprite with four costumes, we
  could have made three sprites: <code>LeftMole</code>, <code>CentreMole</code>, and
  <code>RightMole</code>, each with two costumes: an “empty hole” one and a “mole
  popping out of hole” one.  The media library has suitable
  graphics files, in the "Single mole" bundle.  See if you can re-write
  the game this way.  Some questions you might want to think about:<ul>
<li>Should it be possible for more than one mole to be out of its
  hole at the same time?</li>
<li>How will you keep track of the score?  One way would be to use a
  variable which belongs to the stage.</li>
<li>With one sprite per mole/hole, you could let the player click or
  tap to hit a hole, instead of using the keyboard.  Do you think
  that would be better?  Can you change the code so it reacts to
  the sprite being clicked?</li>
</ul>
</li>
</ul></div></div>